package xyz.showbooks.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.dfm.util.WebUtil;
import xyz.dfm.vo.PageResult;
import xyz.showbooks.mapper.PermissionMapper;
import xyz.dfm.pojo.Permission;
import xyz.dfm.pojo.Role;
import xyz.showbooks.service.PermissionService;


import java.util.ArrayList;
import java.util.List;

/**
 * @author: Dsi
 * @date: 2022/2/24 11:01
 * @description:
 */
@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Override
    public PageResult findRoleById(PageResult pageResult) {
        //根据用户id查询其所拥有所有角色
        List<Role> roles=permissionMapper
                .findRoleById(WebUtil.utilPagePojo(pageResult));
        for (Role role:roles
             ) {
            role.setChildren(
                    permissionMapper.findPermissionByRole(role.getRoleId()));
        }
        if(roles==null){
            return pageResult;
        }
        return pageResult.setRows(roles);
    }
    //根据用户id查询其所拥有所有权限
    @Override
    public List<String> selectUserPermission(Integer userId) {
        List<String> list=new ArrayList<>();
        List<Permission> permissions = permissionMapper.selectUserPermission(userId);
        for (Permission permission:permissions
        ) {
            list.add(permission.getPermissionValue());
        }return list;
    }

    @Override
    public List<String> findPermissionById(Integer roleId) {
        return permissionMapper.findPermissionById(roleId);
    }
}
